package com.sskj.one.login;

import android.content.Context;
import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hjq.toast.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.one.MainActivity;
import com.sskj.one.R;
import com.sskj.one.bean.AccountBean;
import com.sskj.one.component.DaggerUserDataComponent;
import com.sskj.one.util.EditUtil;

import javax.inject.Inject;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;

/**
 * @author Hey
 * Create at  2019/05/21
 */
@Route(path = RConfig.APP_LOGIN)
public class LoginActivity extends BaseActivity<LoginPresenter> {

    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.iv_see)
    ImageView ivSee;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.to_forget_pwd)
    TextView toForgetPwd;
    @BindView(R.id.to_register)
    ConstraintLayout toRegister;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @Inject
    UserViewModel userViewModel;

    private UserData userData;

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
//                MainActivity.start(LoginActivity.this);
//                finish();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.app_activity_login;
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(300, ivClose, this::finish);
        ClickUtil.click(300, ivSee, () -> {
            EditUtil.togglePs(etPwd, ivSee);
        });
        ClickUtil.click(300, toRegister, () -> {
            RegisterActivity.start(LoginActivity.this);
        });
        ClickUtil.click(300, toForgetPwd, () -> {
            ForgetPwdActivity.start(LoginActivity.this);
        });
        ClickUtil.click(800, btnLogin, () -> {
            if (isEmpty(etMobile)) {
                toastHint(etMobile);
                return;
            }
            if (!CheckUtil.isMobile(getText(etMobile))) {
                ToastUtils.show("手机号格式错误");
                return;
            }
            if (isEmpty(etPwd)) {
                ToastUtils.show("请输入登录密码");
                return;
            }
            if (!CheckUtil.isPayPwd(getText(etPwd))) {
                toastHint(etPwd);
                return;
            }
            mPresenter.login(getText(etMobile), getText(etPwd));

        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public void loginSuccess(AccountBean data, String mobile) {
        if (data == null) {
            return;
        }
        JPushInterface.setAlias(App.INSTANCE, 0, data.getAccount());
        SPUtil.put(SPConfig.LOGIN, true);
        SPUtil.put(SPConfig.MOBILE, mobile);
        SPUtil.put(SPConfig.ACCOUNT, data.getAccount());
        SPUtil.put(SPConfig.TOKEN, data.getToken());
        SPUtil.put(SPConfig.MUSIC_ONE, "1");
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put("token", data.getToken());
        httpHeaders.put("account", data.getAccount());
        httpHeaders.put("systemType", "ANDROID");
        OkGo.getInstance().addCommonHeaders(httpHeaders);
        userViewModel.update();
        MainActivity.start(LoginActivity.this);
        finish();
    }
}
